import { getuserUid } from '@/utils/auth.js'
import { paying } from '@/utils/pay.js'

export default async function () {
    let payType = this.payOrderPayWayActiveIndex === 0 ? 2 :
        this.payOrderPayWayActiveIndex === 1 ? 1 :
            3
    let params = {
        createBy: getuserUid(),
        userUid: getuserUid(),
        bUserUid: this.businessUserUid,
        // 项目名称
        orderName: this.payOrderProjectName,
        // 地址UID
        UserAddressUid: this.addressuid,
        // 预约日期
        serviceTime: this.payOrderConventionDate.map(e => e.replace(/\//g, '-')),
        // 上门时间
        // TODO 
        falstTime: `${this.payOrderConventionDate
            .sort((a, b) => new Date(a) - new Date(b))[0]
            .replace(/\//g, '-')
            } ${this.payOrderUpDoorDateCelectActive}`,
        // 需求效果
        // skillUid: this.orightSkill.find(e => this.demandList.includes(e.name)).uid,
        skillUid: this.payOrderDemandUid,
        // 订单类型  （固定1）
        orderType: 1,
        payType: payType,
    }
    if (payType === 3) {
        console.log(this.payWayList)
        console.log(this.payOrderPayWayActiveIndex)
        console.log('uid ', this.payWayList[this.payOrderPayWayActiveIndex])
        params.bankCardUid = this.payWayList[this.payOrderPayWayActiveIndex].uid
    }
    let payResponse

    if (this.payRun) {
        console.log('锁')
        return
    }
    // TODO 锁
    console.log('--支付中---')
    console.log(params)
    if (!this.addressuid) {
        this.$toast('地址未选择')
        return
    }
    if (!this.payOrderUpDoorDateCelectActive) {
        this.$toast('上门时间未选择')
        return
    }
    if (!this.payOrderProjectName) {
        this.$toast('项目名称不能为空')
        return
    }
    if (!params.skillUid) {
        this.$toast('需求效果未选择')
        return
    }
    if (!params.orderType) {
        this.$toast('支付方式未选择')
        return
    }

    // 支付失败
    let payFail = () => {
        this.payRun = false
    }
    this.payRun = true
    try {
        payResponse = await HTTP('app/buser/createOrder', params)
        if (payResponse.code !== 200) {
            this.$toast(payResponse.msg || '异常')
            payFail()
            return
        }
        console.log(JSON.stringify(payResponse.data))
        let outTradeNo = payResponse.msg
        this.outTradeNo = outTradeNo
        this.payRun = false
        paying({
            url: 'app/buser/createOrder',
            params
        }, payType, payResponse.data, this.outTradeNo)
    } catch (e) {
        console.log(e)
        this.$toast('数据开小差了')
        payFail()
    }
}